import LXCOM from '../../common/common.js'
import abspkg from '../../graph/action/absaction.js'

import UtilFuncs from '../../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import {LGXEnums}  from '../../common/sysenum.js';


class ArcAction extends abspkg.AbsGraphAction {
    constructor(graph) {
        super(graph);
        this.sureRadius = false;
        this.sureStPointFlag = false;
        this.prePt = { x:0, y:0 };
    }

    modifyByCMD(isNew, cmd) {
        let rst = null;
        var graph = this.getRefGraph();
        var position = graph.getPos();

        if (cmd == "e") {
            graph.setFinish(1);
            graph.calControlPoint();
            graph.mouseDownTime = 0;

            let cmd = { type: 'e', value: "" };
            let nextTips = "";
            let appendToPreCMD = "";
            rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: true };
        }
        else {
            let angle = 0;
            let xdesc = "";
            let ydesc = ""
            let radius = -1;
            let coordLst = cmd.split(",");
            if (coordLst.length == 2) {
                xdesc = coordLst[0];
                ydesc = coordLst[1];
            }
            else if (coordLst.length == 1) {
                if (cmd.indexOf("r") == 0) {
                    let strRadius = cmd.substr(1, cmd.length - 1);
                    radius = parseFloat(strRadius);
                }
                else {
                    angle = parseFloat(cmd);
                }
            }

            if (isNew && coordLst.length != 2) {
                return null;
            }

            let prePt = { x: 0, y: 0 };
            if (graph.plotMode == LGXEnums.ArcPlotMode.CenterAngleMode) {
                prePt.x = position.x;
                prePt.y = position.y;
            }
            else if (graph.plotMode == LGXEnums.ArcPlotMode.Point3Mode) {
                if (graph.mouseDownTime == 1) {
                    prePt.x = graph.stPoint.x;
                    prePt.y = graph.stPoint.y;
                }
                else if (graph.mouseDownTime == 2) {
                    prePt.x = graph.midCtrlPoint.x;
                    prePt.y = graph.midCtrlPoint.y;
                }
            }

            let isRelateCoord = false;
            let x = parseFloat(xdesc);
            let XRelateIdx = xdesc.indexOf("@");
            if (XRelateIdx >= 0) {
                let tmp = xdesc.substr(XRelateIdx + 1, xdesc.length - XRelateIdx - 1);
                x = prePt.x + parseFloat(tmp);
                isRelateCoord = true;
            }

            let y = parseFloat(ydesc);
            if (isRelateCoord) {
                y = prePt.y + y;
            }

            if (isNew) {
                if (xdesc.indexOf("@") >= 0) {
                    return null;
                }
                if (graph.plotMode == LGXEnums.ArcPlotMode.CenterAngleMode) {
                    position.x = x;
                    position.y = y;
                    graph.mouseDownTime = 1;
                    graph.setStart(1);

                    let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                    let cmd = { type: 'draw_lmousedown', value: coordDecs };
                    let nextTips = "输入第二个点坐标或在图中单击确定半径及起点角度:x,y,相对坐标使用:@x,y。输入半径使用:r+半径值（如:r20）。结束（e）";
                    let appendToPreCMD = coordDecs;
                    rst = { cmdObj: cmd, nextTips, appendToPreCMD };
                }
                else {
                    graph.setStart(1);
                    graph.setStPoint({ x, y });
                    graph.mouseDownTime = 1;

                    let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                    let cmd = { type: 'draw_lmousedown', value: coordDecs };
                    let nextTips = "输入或在图中单击确定第二个点坐标:x,y,相对坐标使用:@x,y";
                    let appendToPreCMD = coordDecs;
                    rst = { cmdObj: cmd, nextTips, appendToPreCMD };
                }
            }
            else if (graph.getStart() == 1 && graph.mouseDownTime == 1) {
                if (radius > 0) {
                    if (graph.plotMode == LGXEnums.ArcPlotMode.CenterAngleMode) {
                        position.radius = radius;
                        let cmd = { type: 'input_radius', value: radius };
                        let nextTips = "输入起始角度值。使用当前位置直接回车（enter）";
                        let appendToPreCMD = "";
                        rst = { cmdObj: cmd, nextTips, appendToPreCMD };
                        this.sureRadius = true;
                    }
                }
                else if (cmd == "enter") {
                    if (graph.plotMode == LGXEnums.ArcPlotMode.CenterAngleMode) {
                        graph.mouseDownTime == 2;
                        let coordDecs = position.stAngle;
                        let cmd = { type: 'draw_lmousedown', value: coordDecs };
                        let nextTips = "输入弧长角度值确定终止角度,使用当前位置直接回车结束（e）";
                        let appendToPreCMD = "enter";
                        rst = { cmdObj: cmd, nextTips, appendToPreCMD };

                        this.sureStPointFlag = true;
                        graph.mouseDownTime = 2;
                    }
                }
                else {
                    let curentPt = { x, y };
                    if (graph.plotMode == LGXEnums.ArcPlotMode.CenterAngleMode) {
                        if (!this.sureRadius && !this.sureStPointFlag) {
                            let dis = funcs.distancePoint(position, curentPt, 1);
                            position.radius = dis;
                            let vect = funcs.getVector(position, curentPt);
                            position.stAngle = vect.angleFrmXAxisPositiveDir();

                            let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                            let cmd = { type: 'draw_lmousedown', value: coordDecs };
                            let nextTips = "输入弧长角度值确定终止角度,使用当前位置直接回车结束（e）";
                            let appendToPreCMD = coordDecs;
                            rst = { cmdObj: cmd, nextTips, appendToPreCMD };

                            this.sureRadius = true;
                            this.sureStPointFlag = true;
                            graph.mouseDownTime = 2;
                        }
                        else if (this.sureRadius && !this.sureStPointFlag) {
                            position.stAngle = angle;
                            let cmd = { type: 'input_angle', value: angle };
                            let nextTips = "输入弧长角度值确定终止角度,使用当前位置直接回车结束（e）";
                            let appendToPreCMD = "";
                            rst = { cmdObj: cmd, nextTips, appendToPreCMD };
                            this.sureStPointFlag = true;
                            graph.mouseDownTime = 2;
                        }

                    }
                    else if (graph.plotMode == LGXEnums.ArcPlotMode.Point3Mode) {
                        graph.setMidPoint({ x, y });
                        graph.mouseDownTime = 2;

                        let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                        let cmd = { type: 'draw_lmousedown', value: coordDecs };
                        let nextTips = "输入或在图中单击确定第三个点坐标:x,y,相对坐标使用:@x,y";
                        let appendToPreCMD = coordDecs;
                        rst = { cmdObj: cmd, nextTips, appendToPreCMD };
                    }
                }
            }
            else if (graph.getStart() == 1 && graph.mouseDownTime == 2) {
                if (graph.plotMode == LGXEnums.ArcPlotMode.CenterAngleMode) {
                    position.endAngle = position.stAngle + angle;
                    graph.mouseDownTime = 3;
                    graph.calControlPoint();
                    graph.setFinish(1);

                    let cmd = { type: 'e', value: "" };
                    let nextTips = "";
                    let appendToPreCMD = "e";
                    rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: true };
                }
                else if (graph.plotMode == LGXEnums.ArcPlotMode.Point3Mode) {
                    graph.setEndPoint({ x, y });
                    graph.mouseDownTime = 3;
                    graph.setFinish(1);
                    graph.calControlPoint();

                    let cmd = { type: 'e', value: "" };
                    let nextTips = "";
                    let appendToPreCMD = "e";
                    rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: true };
                }
            }
        }
        graph.calScope(1);
        return rst;
    }

    act2Mouse(mosEvnt, mousePt) {
        var graph = this.getRefGraph();
        if (graph == null) {
            return LGXEnums.LGXPlotState.ErrorState;
        }
        let state = LGXEnums.LGXPlotState.NoneState;
        if (graph.plotMode == LGXEnums.ArcPlotMode.CenterAngleMode) {
            state = this.act2MouseCenterAngleMode(mosEvnt, mousePt);
        }
        else if (graph.plotMode == LGXEnums.ArcPlotMode.Point3Mode) {
            state = this.act2Mouse3PointMode(mosEvnt, mousePt);
        }
        graph.calScope(1);
        return state;
    }

    act2MouseCenterAngleMode(mosEvnt, mousePt) {
        var graph = this.getRefGraph();
        var state = LGXEnums.LGXPlotState.NoneState;
        var position = graph.getPos();
        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (graph.getStart() == 0) {
                graph.setStart(1);
                position.x = mousePt.x;
                position.y = mousePt.y;
                state = LGXEnums.LGXPlotState.OKState;
                graph.mouseDownTime = 1;
            }
            else if (graph.getStart() == 1 && graph.getFinish() == 0) {
                if (graph.mouseDownTime == 1) {
                    let dis = funcs.distancePoint(position, mousePt, 1);
                    if (!this.sureRadius) {
                        position.radius = dis;
                    }
                    let vect = funcs.getVector(position, mousePt);
                    position.stAngle = vect.angleFrmXAxisPositiveDir();
                    graph.mouseDownTime = 2;
                    state = LGXEnums.LGXPlotState.OKState;
                }
                else if (graph.mouseDownTime == 2) {
                    let vect = funcs.getVector(position, mousePt);
                    position.endAngle = vect.angleFrmXAxisPositiveDir();
                    graph.mouseDownTime = 3;
                    state = LGXEnums.LGXPlotState.FinistState;
                    graph.calControlPoint();
                    graph.setFinish(1);
                }
            }

        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.mouseMove) {
            if (graph.getStart() == 1 && graph.getFinish() == 0) {
                if (graph.mouseDownTime == 1) {
                    graph.setDisAuxPoint(mousePt);
                }
                else if (graph.mouseDownTime == 2) {
                    let vect = funcs.getVector(position, mousePt);
                    position.endAngle = vect.angleFrmXAxisPositiveDir();
                }
            }

            state = LGXEnums.LGXPlotState.OKState;
        }
        return state;
    }

    act2Mouse3PointMode(mosEvnt, mousePt) {
        var graph = this.getRefGraph();
        var state = LGXEnums.LGXPlotState.NoneState;
        var position = graph.getPos();
        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (graph.getStart() == 0) {
                graph.setStart(1);
                graph.setStPoint(mousePt);
                state = LGXEnums.LGXPlotState.AppendPtState;
                graph.mouseDownTime = 1;
            }
            else if (graph.getStart() == 1 && graph.getFinish() == 0) {
                if (graph.mouseDownTime == 1) {
                    graph.setMidPoint(mousePt);
                    graph.mouseDownTime = 2;
                    state = LGXEnums.LGXPlotState.OKState;
                }
                else if (graph.mouseDownTime == 2) {
                    graph.setEndPoint(mousePt);
                    graph.mouseDownTime = 3;
                    state = LGXEnums.LGXPlotState.FinistState;
                    graph.setFinish(1);
                    graph.calControlPoint();
                }
            }
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.mouseMove) {
            if (graph.getStart() == 1 && graph.getFinish() == 0) {
                if (graph.mouseDownTime == 1) {
                    graph.setMidPoint(mousePt);
                    state = LGXEnums.LGXPlotState.OKState;
                }
                else if (graph.mouseDownTime == 2) {
                    graph.setEndPoint(mousePt);
                    graph.calControlPoint();
                }
            }
            state = LGXEnums.LGXPlotState.OKState;
        }
        graph.calScope(1);
        return state;
    }
}

export default { ArcAction }
